package com.zimi.controller;

import com.zimi.model.*;
import com.zimi.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api("流程管理")
@Controller
@RequestMapping("wf/workflow")
public class WfWorkflowApi {

    @Autowired
    private WfWorkflowService service;
    @Autowired
    private WfActivityService wfActivityService;
    @Autowired
    private WfActivityLineService wfActivityLineService;
    @Autowired
    private WfFormService wfFormService;
    @Autowired
    private WfWorkflowApplicationService wfWorkflowApplicationService;

    @PostMapping("list")
    @ResponseBody
    @ApiOperation("查询所有")
    public Map selectAll(WfWorkflow record) {
        Map returnMap = new HashMap();
        returnMap.put("OK", "OK");
        returnMap.put("code", 200);
        returnMap.put("rows", service.selectAll(record));
        returnMap.put("List", service.selectAll(record));
        return returnMap;
    }

    @PostMapping("getByFormId")
    @ResponseBody
    @ApiOperation("根据表单查询流程")
    public Map getByFormId(int fid) {
        Map returnMap = new HashMap();
        returnMap.put("OK", "OK");
        returnMap.put("code", 200);
        returnMap.put("List", service.selectWfWorkflowByFormId(fid));
        return returnMap;
    }

    @PostMapping("{id}")
    @ResponseBody
    @ApiOperation("根据Id查询信息")
    public Object selectByPrimaryKey(@PathVariable("id") int id) {
        Map<String, Object> objectMap = new HashMap<>();
        try {
            objectMap.put("wfWorkflow", service.selectByPrimaryKey(id));
        } catch (Exception e) {
            objectMap.put("Ok", "error");
            objectMap.put("code", 400);
            objectMap.put("message", e.getMessage());
        }
        objectMap.put("Ok", "OK");
        objectMap.put("code", 200);
        return objectMap;
    }

    @DeleteMapping("remove")
    @ResponseBody
    @ApiOperation("根据Id删除")
    public boolean deleteByPrimaryKey(WfWorkflow record) {
        return service.deleteByPrimaryKey(record.getWfId()) > 0 ? true : false;
    }

    @PostMapping("record")
    @ResponseBody
    @ApiOperation("添加")
    public boolean insert(WfWorkflow record) {
        return service.insertSelective(record) > 0 ? true : false;
    }

    @PostMapping("update")
    @ResponseBody
    @ApiOperation("根据条件修改信息")
    public Object updateByPrimaryKey(WfWorkflow record) {
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            service.updateByPrimaryKeySelective(record);
        } catch (Exception e) {
            map.put("OK", "error");
            map.put("code", 400);
            map.put("message", e.getMessage());
        }
        map.put("OK", "OK");
        map.put("code", 200);
        return map;
    }

    @GetMapping("toDesigner")
    @ApiOperation("查询流程信息")
    public String toDesigner(int wfId, Model model) {
        WfWorkflow wfWorkflow = service.selectByPrimaryKey(wfId);
        if (wfWorkflow == null) return "error page";
        Map map = new HashMap();
        map.put("wfId", wfId);
        map.put("wfCode", wfWorkflow.getWfCode());
        map.put("moduleId", wfWorkflow.getModuleId());
        //map.put("ognid", curUser.getOgnId());
        model.addAllAttributes(map);
        return "/base/workFlow/workFlow/workflowDesigner";
    }

    @PostMapping("listWfWorkflow")
    @ResponseBody
    @ApiOperation("初始化机构")
    public Object listWfWorkflow(WfWorkflowVO record) {
        Map<String, Object> objectMap = new HashMap<>();
        objectMap.put("Ok", "OK");
        objectMap.put("code", 200);
        objectMap.put("rows", service.selectAll(record));
        return objectMap;
    }

    @PostMapping("addNewWorkFlow")
    @ResponseBody
    @ApiOperation("新建流程")
    public Object addNewWorkFlow(WfWorkflow wfWorkflow) {
        Map<String, Object> objectMap = new HashMap<>();
        int wfId = service.saveNewWorkFlow(wfWorkflow);    //保存流程
        if (wfId == 0) {
            objectMap.put("message", "此表单已经绑定到别的流程，绑定失败！");
            objectMap.put("OK", "error");
        } else {
            Map<String, Object> map = wfActivityService.save_createStartAndEndActivity(wfId);   //创建开始和结束活动
            wfActivityLineService.save_createLineFromStart(wfId, (WfActivity) map.get("startActivity"), (WfActivity) map.get("firstActivity")); //创建开始和001连线
            objectMap.put("OK", "OK");
        }

        objectMap.put("code", 200);
        return objectMap;
    }

    @PostMapping("getWfForm")
    @ResponseBody
    @ApiOperation("获取绑定保单数据")
    public Object getWfForm(WfActivityLine wfActivityLine) {
        Map<String, Object> objectMap = new HashMap<>();
        try {
            WfWorkflow wf = service.selectByPrimaryKey(wfActivityLine.getWfId());
            List<WfFormItem> wfFormItemList = null;
            if (wf != null && wf.getFormEditId() != null) {
                WfForm wfForm = wfFormService.selectByPrimaryKey(wf.getFormEditId());
                if (wfForm != null && wfForm.getFormHtml() != null && wfForm.getFormHtml().length() > 0) {
                    wfFormItemList = wfWorkflowApplicationService.getFormItems(wfForm.getFormHtml());
                } else {
                    wfFormItemList = Collections.emptyList();
                }
            } else {
                wfFormItemList = Collections.emptyList();
            }
            objectMap.put("formField", wfFormItemList);
            List<WfActivityLine> activityList = wfActivityLineService.selectAll(wfActivityLine);
            objectMap.put("List", wfFormItemList);
        } catch (Exception e) {
            objectMap.put("Ok", "error");
            objectMap.put("code", 400);
        }

        objectMap.put("Ok", "OK");
        objectMap.put("code", 200);
        return objectMap;
    }

    @PostMapping("moveUp")
    @ResponseBody
    @ApiOperation("上移")
    public Object moveUp(Integer id) {
        Map<String, Object> map = new HashMap<String, Object>();
        service.moveUp(id);
        map.put("OK", "OK");
        map.put("code", 200);
        return map;
    }

    @PostMapping("moveDown")
    @ResponseBody
    @ApiOperation("下移")
    public Object moveDown(Integer id) {
        Map<String, Object> map = new HashMap<String, Object>();
        service.moveDown(id);
        return map;
    }

    @PostMapping("getWorkFlowState")
    @ResponseBody
    @ApiOperation("查询流程状态")
    public Object getWorkFlowState(Integer id) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("flag", service.getWorkFlowState(id));
        map.put("OK", "OK");
        map.put("code", 200);
        return map;
    }

    @PostMapping("getWfFormByFormId")
    @ResponseBody
    @ApiOperation("获取绑定保单数据")
    public Object getWfFormByFormId(Integer id) {
        Map<String, Object> objectMap = new HashMap<>();
        try {
            List<WfFormItem> wfFormItemList = null;
            if (id != null && id != null) {
                WfForm wfForm = wfFormService.selectByPrimaryKey(id);
                if (wfForm != null && wfForm.getFormHtml() != null && wfForm.getFormHtml().length() > 0) {
                    wfFormItemList = wfWorkflowApplicationService.getFormItems(wfForm.getFormHtml());
                } else {
                    wfFormItemList = Collections.emptyList();
                }
            } else {
                wfFormItemList = Collections.emptyList();
            }
            objectMap.put("formField", wfFormItemList);
        } catch (Exception e) {
            objectMap.put("Ok", "error");
            objectMap.put("code", 400);
        }

        objectMap.put("Ok", "OK");
        objectMap.put("code", 200);
        return objectMap;
    }

    @PostMapping("getGarbage")
    @ResponseBody
    @ApiOperation("查询所有垃圾回收站")
    public Map getGarbage(WfWorkflow record) {
        Map returnMap = new HashMap();
        returnMap.put("OK", "OK");
        returnMap.put("code", 200);
        returnMap.put("rows", service.selectAll(record));
        returnMap.put("List", service.selectAll(record));
        return returnMap;
    }

    @PostMapping("batchsRecoverWfWorkflow")
    @ResponseBody
    @ApiOperation("垃圾回收站批量处理")
    public Object batchsRecoverWfWorkflow(String wfId) {
        String ids = wfId.replace("\"", "");
        String[] strarray = ids.split(",");
        Map returnMap = new HashMap();
        try {
            for (String id : strarray) {
                WfWorkflow wfworkflow = service.selectByPrimaryKey(Integer.parseInt(id));
                wfworkflow.setDeleteFlag(1);
                service.updateByPrimaryKeySelective(wfworkflow);
                returnMap.put("OK", "OK");
                returnMap.put("code", 200);
            }
        } catch (Exception e) {
            returnMap.put("OK", "error");
            returnMap.put("code", 400);
            returnMap.put("message", e.getMessage());
        }
        return returnMap;
    }

}
